/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *  
 * Break a single xml file into workspace
 * @Original Author: jin Chen
 * @date: Apr 21, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.xml;

import edu.psu.geovista.app.dataloader.v1.config.auto.Project;
import edu.psu.geovista.app.dataloader.v1.session.ProjectBuilder;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class Upgrader {
   // public static final  String WORKSPACE="/.pcp/workspace/";
    private String  workspace;
    JaxbPathsXMLFs pathInfos;
    ProjectsXMLFileSystem projs;

    public Upgrader() throws IOException {
        //String userHomePath=FileUtils.getUserHome() ;
        //String workspace="c:/temp/estat/" ;//userHomePath+Upgrader.WORKSPACE;
       //this.setWorkspace(workspace);


        //generate projects.xml
        projs=new Dom4jProjectsXMLFs();

        File root = ProjectBuilder.getWorkspaceDir();;//getWorkspaceDir();
        /*File projects=new File(root,ProjectBuilder.PROJECTS);
        if(! projects.exists() ){
            try {
                projects.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }*/
        File projectsFile = ProjectBuilder.getProjectsFile();
        this.projs.setFile(projectsFile);

        //load old file
        File rootDir = ProjectBuilder.getUserHomeConfigDir();
        File projInfo=new File(rootDir ,ProjectBuilder.projfoFileName);
        pathInfos=new JaxbPathsXMLFs();
        pathInfos.setFile(projInfo);
        pathInfos.load();//todo need consider if no old file exists
    }

   /* private File getworkspace(){
        String workspace="c:/temp/estat/";
        File dir=new File(workspace);
        dir.mkdirs();
    }*/

    public void upgrade(){
        List projList = pathInfos.getSortProjectList();

        Set projSet=new HashSet();

        //generate individual project root
        for (Iterator iterator = projList.iterator(); iterator.hasNext();) {
            Project xmlproject = (Project) iterator.next();
            edu.psu.geovista.app.dataloader.v1.config.model.Project myproject = JaxbPathInfoConvertor.convert(xmlproject);
            generateAProject(myproject);    //generate a project root and path info for it
            projSet.add(myproject);//// for generate projects.xml
        }


        projs.setProjects(projSet);
        projs.save();

    }

    private Map <String,File> generateProjectRoots(List<String> projnames){
        File root=new File(this.getWorkspace());
        Map projs=new HashMap();
        for (Iterator iterator = projnames.iterator(); iterator.hasNext();) {
            String fn = (String) iterator.next();
            File dir=new File(root,fn);
            root.mkdir();
            projs.put(fn,dir);
        }
        return projs;
    }
    /**
     * generate a project root and path info for it
     * @param proj
     */
    private void generateAProject(edu.psu.geovista.app.dataloader.v1.config.model.Project proj){
        String id = proj.getID();
        File root = getWorkspaceDir();
        //project root is a folder with name=id
        File projroot=new File(root,id);
        if(!projroot.exists()){
            projroot.mkdir();
        }

        File pathsFile=new File(projroot,ProjectBuilder.PATHS);
        Dom4jPathsXMLFs xmlfs=new Dom4jPathsXMLFs();
        xmlfs.setFile(pathsFile);
        xmlfs.setProject(proj);
        try {
            xmlfs.save();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        /*
        Document doc = PathsToDom4j.convert(proj);
        try {
            XMLParserDom4j.saveDocument(doc,path);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
*/

    }

    private File getWorkspaceDir() {
        File root=new File(this.getWorkspace());
        if(!root.exists() ){
            root.mkdirs();
        }
        return root;
    }

    public String getWorkspace() {
        return workspace;
    }

    public void setWorkspace(String workspace) {
        this.workspace = workspace;
    }

    public static void main(String[] args) {
        Upgrader up=null;
        try {

            String workspace = ProjectBuilder.getWorkspaceRoot();
             File wsdir=new File(workspace);
            if(! wsdir.exists() ){
            Upgrader ug=new Upgrader();
            ug.setWorkspace(workspace);
            ug.upgrade();
        }


        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }



    }
}
